package com.hzp.tool.common.ui.frame;

import com.hzp.tool.common.ui.component.PropertyComponent;

import javax.swing.*;
import javax.swing.text.JTextComponent;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.HashMap;
import java.util.Map;

import static com.hzp.tool.common.util.UIUtils.centerComponentOnScreen;

public abstract class BaseFrame extends JFrame {

    static {
        try {
            UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (Exception e) {
        }
    }

    private Map<String, PropertyComponent> propertyComponents = new HashMap<>();
    private BaseFrame thisFrame = this;
    private FrameProperties frameProperties;


    public BaseFrame(String title) {
        this(title, 800, 600);
    }

    public BaseFrame(String title, int width, int height) {
        setTitle(title);
        setSize(width, height);
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                int option = JOptionPane.showConfirmDialog(thisFrame, "确定要关闭程序吗？", "", JOptionPane.YES_NO_OPTION);
                if (option != JOptionPane.YES_OPTION) {
                    return;
                }
                try {
                    saveProperties(frameProperties);
                } finally {
                    System.exit(0);
                }
            }
        });
    }

    public void launch() {
        JPanel panel = new JPanel(new BorderLayout());
        Component header = createHeader();
        if (header != null) {
            panel.add(header, BorderLayout.NORTH);
        }
        Component content = createContent();
        if (content != null) {
            panel.add(content, BorderLayout.CENTER);
        }
        setContentPane(panel);
        centerComponentOnScreen(this);
        frameProperties = loadProperties();
        setVisible(true);
    }

    public void destroy() {
        setVisible(false);
        dispose();
    }

    public void refresh() {
        getContentPane().revalidate();
    }

    protected abstract Component createHeader();

    protected abstract Component createContent();

    protected void addPropertyComponent(String key, Component component) {
        propertyComponents.put(key, new PropertyComponent(component, ""));
    }

    protected void addPropertyComponent(String key, Component component, String deft) {
        propertyComponents.put(key, new PropertyComponent(component, deft));
    }

    private FrameProperties loadProperties() {
        FrameProperties properties = new FrameProperties(this);
        propertyComponents.forEach((key, component) -> component.setProperty(properties.getString(key)));
        onLoadProperties(properties);
        return properties;
    }

    private void saveProperties(FrameProperties properties) {
        propertyComponents.forEach((key, component) -> properties.setValue(key, component.getProperty()));
        onSaveProperties(properties);
        properties.save();
    }

    protected void onLoadProperties(FrameProperties properties) {

    }

    protected void onSaveProperties(FrameProperties properties) {
    }

    public FrameProperties getFrameProperties() {
        return frameProperties;
    }

    protected String getTextFrom(JTextComponent comp, String errMsg) throws Exception {
        String text = comp.getText().trim();
        if (text.length() == 0 && errMsg != null && errMsg.length() > 0) {
            throw new Exception(errMsg);
        }
        return text;
    }

    protected void alert(String message) {
        JOptionPane.showMessageDialog(thisFrame, message, "提示信息", JOptionPane.INFORMATION_MESSAGE);
    }

    protected String prompt(String message) {
        return JOptionPane.showInputDialog(thisFrame, message, "请输入信息", JOptionPane.INFORMATION_MESSAGE);
    }

    protected BaseFrame getBaseFrame(){
        return thisFrame;
    }
}
